<?php
namespace app\manage\controller;

class Notice extends Base
{
    //礼包列表
    public function index(){
        if(request()->isAjax()){
            $page = input('page');
            $limit = input('limit');
            $id = input('id');
            $type = input('type');

            $where['status'] = ['<>', -1];
            !empty($type) && $where['type']=$type;
            !empty($id) && $where['id']=$id;

            if( ! $count=model('Notice')->where($where)->count() ){
               $this->layTable();
            }

            $data = model('Notice')
                ->where($where)
                ->limit(($page-1)*$limit, $limit)
                ->order('id desc')
                ->select();
           $this->layTable(0, '礼包列表', $data, $count);
        }else{
            return view('', ['notice_type'=>config('notice_type')]);
        }
    }

    //添加
    public function add(){
        if( request()->isAjax() ){
            $data = input();

            unset($data['file']);

            $addRs = model('Notice')->save( $data );
            $addRs ? $this->layJson(0, '添加成功') : $this->layJson(1, '添加失败，请重试');
        }else{
            return view('', ['notice_type'=>config('notice_type')]);
        }
    }

    //编辑
    public function edit(){
        if( request()->isAjax() ){
            $data = input();

            unset($data['file']);

            if(!empty($data['datetime'])){
                $datetime = explode(' - ',$data['datetime']);
                unset($data['datetime']);
                $data['start_time'] = $datetime[0];
                $data['end_time'] = $datetime[1];
            }

            $editRs = model('Notice')->isUpdate(true)->save( $data );
            $editRs!==false ? $this->layJson(0, '编辑成功') : $this->layJson(1, '编辑失败，请重试');
        }else{
            $find = model('Notice')->find( input('id') );
            return view('', ['find'=>$find, 'notice_type'=>config('notice_type')]);
        }
    }

    //删除
    public function del(){
        $data['id'] = input('id');
        $data['status'] = -1;
        $delRs = model('Notice')->isUpdate(true)->save( $data );
        $delRs!==false ? $this->layJson(0, '删除成功') : $this->layJson(1, '删除失败，请重试');
    }
}